package com.kvest.book_provider_client;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

public class BookProviderClientActivity extends Activity {
    public static final String AUTHORITY = "com.kvest.provider.BookProvider";
    public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/books");

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    public void onAddClick(View view) {
        ContentValues values = new ContentValues();
        values.put(BookTableMetaData._ID, "1");
        values.put(BookTableMetaData.BOOK_NAME, "Book");
        values.put(BookTableMetaData.BOOK_AUTHOR,"Kvest");

        ContentResolver resolver = getContentResolver();

        Uri uri = resolver.insert(CONTENT_URI, values);

        values.clear();
        values.put(BookTableMetaData._ID, "2");
        values.put(BookTableMetaData.BOOK_NAME, "Book 2");
        values.put(BookTableMetaData.BOOK_AUTHOR,"Kvest 2");

        Uri uri2 = resolver.insert(CONTENT_URI, values);

        values.clear();
        values.put(BookTableMetaData._ID, "3");
        values.put(BookTableMetaData.BOOK_NAME, "Book 3");
        values.put(BookTableMetaData.BOOK_AUTHOR,"Kvest 3");

        Uri uri3 = resolver.insert(CONTENT_URI, values);

        Toast.makeText(getApplicationContext(), uri.toString() + "\n" + uri2.toString() +
                "\n" + uri3.toString(), Toast.LENGTH_LONG).show();
        //Log.d("INSERT", uri.toString());
    }

    public void onDeleteClick(View view) {
        Uri uri = ContentUris.withAppendedId(CONTENT_URI, 2);
        int cnt = getContentResolver().delete(uri, null, null);
        Toast.makeText(getApplicationContext(), "cnt=" + cnt, Toast.LENGTH_LONG).show();
    }

    public void onGetClick(View view) {
        String[] projection = new String[]{BookTableMetaData._ID, BookTableMetaData.BOOK_AUTHOR};
        Uri uri = CONTENT_URI;
        Cursor cur = managedQuery(uri, projection ,null,null,null);

        String log = Integer.toString(cur.getColumnCount()) + "\n";

        int index = cur.getColumnIndex(BookTableMetaData.BOOK_AUTHOR);
        for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext())
        {
            log +=  cur.getString(index) + "\n";
        }
        Toast.makeText(getApplicationContext(), log, Toast.LENGTH_LONG).show();
    }

    public static final class BookTableMetaData implements BaseColumns
    {
        private BookTableMetaData(){}
        public static final String BOOK_NAME = "name";
        public static final String BOOK_ISBN = "isbn";
        public static final String BOOK_AUTHOR = "author";
        public static final String CREATED_DATE = "created";
        public static final String MODIFIED_DATE = "modified";
    }
}
